package com.itheima.electronicbusinesssystem;

import java.util.*;

public class Test2 {
    public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
        Product[] products=new Product[4];
        // * 商品类：编号、名字、种类、价格、销量、库存
    products[0]= new Product(getId(products),"苹果14","电子产品",5999.0,199,50);
       products[1]= new Product(getId(products),"苹果14proMAx","电子产品",15000.0,478,100);
       products[2] =new Product(getId(products),"智能机器人","电子产品",35999.0,199,30);
        products[3]=new Product(getId(products),"智能牙刷","电子产品",99.0,199,500);

        System.out.println("1、按照价格排列展示[从小到大]；2、按照库存排列展示[从大到小];按其他键退出查询");
        while (true) {
            int num=scanner.nextInt();
            if (num==1){
                method(products);
            }else if (num==2){
                method2(products);
            }else {
                break;
            }
        }


    }
    //升序
    public static void method( Product[] products){
        Product product=new Product();
        product.sort(products);

        System.out.println(Arrays.toString(products));
    }

    //降序
public static void method2( Product[] products){

    Arrays.sort(products, (str1,str2)->{
        return str2.getAllNums()-str1.getAllNums();
    });

    System.out.println(Arrays.toString(products));
}

    //生成商品编号
    public static String getId( Product[] products){
        Random random=new Random();
        String str=(random.nextInt(9)+1)+"";
        while (true) {

            for (int i = 0; i < 8; i++) {
                int num=random.nextInt(9);
                str+=num;
            }

            int judge = judge(products, str);
            if (judge==-1){
                return str;
            }
        }

    }

    //判断是否重复
    public static int judge( Product[] products,String str){
      if (str.length()==9){

        for (int i = 0; i <products.length; i++) {

                if ( products[i].equals(str)){
                    return i;
            }

           //if (products)
        }

    }
      return -1;
    }





}
